package com.mrd.gtimp.base.action;

import com.mrd.constant.MsgConstants;
import com.mrd.constant.WebPathConstants;
import com.mrd.exception.QueryException;
import com.mrd.framework.config.web.json.Result;
import com.mrd.framework.ui.filter.Filter;
import com.mrd.framework.ui.filter.FilterGroupOption;
import com.mrd.framework.ui.filter.SearchRule;
import com.mrd.framework.ui.filter.SearchRuleOption;
import com.mrd.gtimp.base.entity.Company;
import com.mrd.gtimp.base.entity.SysMsgTemplate;
import com.mrd.gtimp.base.entity.User;
import com.mrd.gtimp.base.service.SysMsgTemplateBiz;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

/**
 * <p> application name: gtimp <p> copyright: Copyright@2012 明睿达科技有限公司
 */

/**
 * @author hejj
 */

@Controller
@RequestMapping("/base")
public class SysMsgTemplateAction {
    private static final long serialVersionUID = 1L;

    @Resource
    private SysMsgTemplateBiz sysMsgTemplateBiz;

    private SysMsgTemplate sysMsgTemplate;

    private List<SysMsgTemplate> sysMsgTemplates;

    /**
     * public String show() { try { page = initPage(TSysMsgTemplateBiz.findEntityCount(filters));
     * Order order = new Order(); order.add(sidx, sord); TSysMsgTemplates = TSysMsgTemplateBiz
     * .findEntityByFilter(page.getCurrentPageStartNo(), page.getPageSize(), filters, order); }
     * catch (Exception e) { e.printStackTrace(); } return SUCCESS; }
     **/

    @Resource
    public void setTSysMsgTemplateBiz(SysMsgTemplateBiz tSysMsgTemplateBiz) {
        this.sysMsgTemplateBiz = tSysMsgTemplateBiz;
    }

    @RequestMapping("/msgTemplate-main")
    public String main(Model model) {
//        title = "通知模板管理";
//        left = "msgTemplate-tree.do";
//        // right = "mod-pms.do";
//        right = "";
        model.addAttribute("title", "通知模板管理");
        model.addAttribute("left", "msgTemplate-tree");
        model.addAttribute("right", "");
        return WebPathConstants.FRAME_WEB_PATH;
    }

    @RequestMapping("/msgTemplate-tree")
    public String tree() {
        return "/gtimp/base/msgTemplate/msgTemplate-tree";
    }

    @RequestMapping("/msgTemplate-find")
    public String find() {
        return "/gtimp/base/msgTemplate/msgTemplate-list";
    }

    @RequestMapping("/msgTemplate-listMsgTemplate")
    public String listMsgTemplate(Filter filters, User user) {
//        try {
        if (StringUtils.isNotEmpty(sysMsgTemplate.getProcess().getBizType())) {
            if (filters == null) {
                filters = new Filter();
                filters.setGroupOp(FilterGroupOption.AND);
            }
            SearchRule searchRule = new SearchRule("process.bizType", SearchRuleOption.eq, sysMsgTemplate
                    .getProcess().getBizType());
            filters.addRule(searchRule);
            if (user.isAdmin()) {
                searchRule = new SearchRule("company", SearchRuleOption.eq, null);
            } else {
                searchRule = new SearchRule("company.id", SearchRuleOption.eq, user
                        .getCompany().getId());
            }
            filters.addRule(searchRule);
//                Order order = new Order();
//            order.add("useState", false);
//            page = initPage(sysMsgTemplateBiz.findEntityCount(filters));
//            sysMsgTemplates = sysMsgTemplateBiz
//                    .findByJqgrid(page.getCurrentPageStartNo(), page.getPageSize(), filters, order);
        }
//        } catch (QueryException e) {
//            e.printStackTrace();
//        }
        //// TODO: 2016/8/13
        return null;
    }

    @RequestMapping("/msgTemplate-manage")
    public String manage() {
        return "/gtimp/base/msgTemplate/msgTemplate-manage";
    }

    @RequestMapping("/msgTemplate-saveOrUpdateSysMsgTemplate")
    @ResponseBody
    public Result saveOrUpdateSysMsgTemplate(User user) {
        Result msg;
        try {
            if (null != sysMsgTemplate) {
                if (user.isAdmin()) {
                    sysMsgTemplate.setCompany(null);
                } else {
                    Company company = new Company();
                    company.setId(user.getCompany().getId());
                    sysMsgTemplate.setCompany(company);
                }
                sysMsgTemplate.setCreateuser(user.getAccount());
                sysMsgTemplate.setCreatetime(new Date());
                sysMsgTemplateBiz.saveOrupdateSysMsgTemplate(sysMsgTemplate);
            }
            msg =Result.newSuccessInstance(MsgConstants.SAVE_SUECCES);
        } catch (Exception e) {
            e.printStackTrace();
            msg =Result.newFailInstance(MsgConstants.SAVE_FAIL);
        }
        return msg;
    }


    @RequestMapping("/msgTemplate-getData")
    public String getData() {
        if (null != sysMsgTemplate) {
            if (StringUtils.isNotEmpty(sysMsgTemplate.getId())) {
                try {
                    sysMsgTemplate = sysMsgTemplateBiz.findEntityById(sysMsgTemplate.getId());
                } catch (QueryException e) {
                    e.printStackTrace();
                }
            }
        }
        //// TODO: 2016/8/13  json
        return null;
    }

    public SysMsgTemplate getSysMsgTemplate() {
        return sysMsgTemplate;
    }

    public void setSysMsgTemplate(SysMsgTemplate sysMsgTemplate) {
        this.sysMsgTemplate = sysMsgTemplate;
    }

    public List<SysMsgTemplate> getSysMsgTemplates() {
        return sysMsgTemplates;
    }

    public void setSysMsgTemplates(List<SysMsgTemplate> sysMsgTemplates) {
        this.sysMsgTemplates = sysMsgTemplates;
    }

}
